<template>
	<view>
		<u-navbar :border-bottom='false' back-icon-size='48' title="联系人信息"></u-navbar>
		<view class="headBtn">
			<view class="headBtn-item" @click="navTo('./editLinkman?title=添加联系人')">
				<u-icon name="man-add" color="#3587F7" size="32"></u-icon><text style="margin-left: 8rpx;">新增联系人</text>
			</view>
			<view class="headBtn-item" @click="goBook">
				<u-icon name="order" color="#3587F7" size="32"></u-icon><text style="margin-left: 8rpx;">从通讯录导入</text>
			</view>
		</view>
		<view class="linkman" v-if="linkList.length">
			<view class="linkman-li" v-for="(link,links) in linkList" :key='links'>
				<u-checkbox v-model="link.checked" shape="circle" @change="changeFn" :name='link.link_id'></u-checkbox>
				<view class="linkman-info">
					<view class="name">
						{{link.link_name}}
					</view>
					<view class="num">
						手机号 {{$u.trim(link.link_tel, 'all')}}
					</view>
				</view>
				<view class="edit" @click="navTo('./editLinkman?title=修改联系人&id='+link.link_id+'&name='+link.link_name+'&mobile='+$u.trim(link.link_tel, 'all'))">
					<u-icon name="edit-pen" color="#3587F7" size="36"></u-icon>
				</view>
			</view>
		</view>
		<view class="noCont" v-else>
			<u-empty text="暂无联系人" mode="list"></u-empty>
		</view>
	</view>
</template>

<script>
	import myRequest from '@/api/index.js'
	export default {
		data() {
			return {
				linkList: [],
				page: '1',
				link_id: '', //选中联系人的id
			}
		},
		onLoad(option) {
			this.link_id = option.id
		},
		onReachBottom() {
			let page = Number(this.page) + 1
			this.page = page.toString()
			this.getContact()
		},
		onShow() {
			this.linkList = []
			this.page = "1"
			this.getContact()
		},
		methods: {
		

			goBook() {
				uni.navigateTo({
					url:'./books'
				})
			},
			// 选择联系人
			changeFn(e) {
				this.linkList.forEach(item => {
					if (item.link_id == e.name) {
						item.checked = true
						this.$store.commit('changeLinkman', item)
						this.link_id = item.link_id
					} else {
						item.checked = false
					}
				})
			},
			// 获取联系人
			async getContact() {
				let {
					page
				} = this
				let res = await myRequest.getMycontacts({
					page: page,
					num: '10'
				})
				if (res.status) {
					res.data.forEach(item => {
						if (this.link_id == item.link_id) {
							item.checked = true
						} else {
							item.checked = false
						}
					})
					this.linkList = [...this.linkList, ...res.data]
				}
			},
			navTo(url) {
				uni.navigateTo({
					url: url
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.headBtn {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 24rpx 28rpx;

		.headBtn-item {
			width: 336rpx;
			height: 76rpx;
			background: #FFFFFF;
			border-radius: 8rpx;
			text-align: center;
			font-size: 28rpx;
			font-family: Segoe UI;
			font-weight: 400;
			line-height: 76rpx;
			color: #3587F7;
		}
	}

	.linkman {
		width: 100%;
		height: auto;
		background: #FFFFFF;
		padding: 0 28rpx;

		.linkman-li {
			border-bottom: 2rpx solid #F2F2F2;
			width: 100%;
			height: 120rpx;
			display: flex;
			align-items: center;

			.linkman-info {
				.name {
					font-size: 32rpx;
					font-family: Segoe UI;
					font-weight: 400;
					color: #333333;
				}

				.num {
					font-size: 28rpx;
					font-family: Segoe UI;
					font-weight: 400;
					color: #999999;
				}
			}

			.edit {
				margin-left: auto;
			}
		}
	}

	.noCont {
		padding-top: 320rpx;
	}
</style>
